﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.Data.Entity;

namespace GPS.Controllers.GPS_DanhMuc
{
    public class GPS_LyDoTuChoiController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        //
        // GET: /GPS_HTCapGPLX/
        #region Index
        public ActionResult Index(ViewModelSLYDOTCSH SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<SLYDOTCSH>(o => (SearchModel.lLYDO_MA == null || o.LYDO_MA.StartsWith(SearchModel.lLYDO_MA))
                                        && (SearchModel.lLYDO_TEN == null || o.LYDO_TEN.StartsWith(SearchModel.lLYDO_TEN))
                                        && (SearchModel.lLYDO_VBPL == null || o.LYDO_VBPL.StartsWith(SearchModel.lLYDO_VBPL))
                                        && (SearchModel.lLYDO_ACTIVE == null || o.LYDO_ACTIVE == SearchModel.lLYDO_ACTIVE));
            ViewBag.TotalRow = entities.Count();
            var model = new List<SLYDOTCSH>();
            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<SLYDOTCSH>(currentEntity));
            }
            Int16 icount = 1;
            foreach (var item in model)
            {
                item.STT = icount;
                icount++;
            }
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }

        [HttpPost]
        public void initialCategorySearchAction(ViewModelSLYDOTCSH viewModel)
        {
            //ViewBag.lstActivedll = new SelectList(_db.GetAll<STRANGTHAI>(), "TRANGTHAI_MA", "TRANGTHAI_TEN");
            //viewModel.listTrangThai = _db.GetAll<STRANGTHAI>();
        }
        #endregion

        //
        // Create: 
        #region create
        public ActionResult Create()
        {
            initialCategoryCreateAction();
            return View(new CrudModelSLYDOTCSH());
        }

        [HttpPost]
        public ActionResult Create(CrudModelSLYDOTCSH model)
        {
            initialCategoryCreateAction();
            if (ModelState.IsValid)
            {
                SLYDOTCSH _model = new SLYDOTCSH();
                GPS.COMMON.Helpers.CopyObject<SLYDOTCSH>(model, ref _model);
                _model.LYDO_MA = GetValue_Indentity();
                _db.Insert<SLYDOTCSH>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryCreateAction()
        {
           // ViewBag.lstActivedll = new SelectList(_db.GetAll<STRANGTHAI>(), "TRANGTHAI_MA", "TRANGTHAI_TEN");
        }

        public string GetValue_Indentity()
        {
            string value = "";
            Int64 valueMax = _db.DbContext.SLYDOTCSHes.ToList().Max(e => Convert.ToInt64(e.LYDO_MA));
            value = (valueMax + 1).ToString();
            return value;
        }
        #endregion

        //
        // Edit: 
        #region Edit
        public ActionResult Edit(string id)
        {
            CrudModelSLYDOTCSH _model = new CrudModelSLYDOTCSH();
            SLYDOTCSH model = _db.GetOne<SLYDOTCSH>(o => o.LYDO_MA == id);
            GPS.COMMON.Helpers.CopyObject<CrudModelSLYDOTCSH>(model, ref _model);
            initialCategoryEditAction(_model);
            return View(_model);
        }
        [HttpPost]
        public ActionResult Edit(CrudModelSLYDOTCSH model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<SLYDOTCSH>(o => o.LYDO_MA == model.LYDO_MA);
                GPS.COMMON.Helpers.CopyObject<SLYDOTCSH>(model, ref _model);
               _db.Update<SLYDOTCSH>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryEditAction(CrudModelSLYDOTCSH view)
        {
            //string selectedValues = _db.GetAll<STRANGTHAI>().Select(x => x.TRANGTHAI_MA = view.LYDO_ACTIVE).FirstOrDefault().ToString();
            //ViewBag.lstActivedll = new SelectList(_db.DbContext.STRANGTHAIs.ToList(), "TRANGTHAI_MA", "TRANGTHAI_TEN", selectedValues);
        }
        #endregion

        #region Detele
        public ActionResult Delete(string id)
        {
            CrudModelSLYDOTCSH _model = new CrudModelSLYDOTCSH();
            SLYDOTCSH model = _db.GetOne<SLYDOTCSH>(o => o.LYDO_MA == id);
            _db.DeleteItem<SLYDOTCSH>(model);
            return RedirectToAction("Index");
        }
        #endregion

        //
        // Detail
        #region Detail
        public ActionResult Detail(string id)
        {
            CrudModelSLYDOTCSH _model = new CrudModelSLYDOTCSH();
            SLYDOTCSH model = _db.GetOne<SLYDOTCSH>(o => o.LYDO_MA == id);
            GPS.COMMON.Helpers.CopyObject<CrudModelSLYDOTCSH>(model, ref _model);
            _model.LYDO_ACTIVE = "Không Hiệu Lực";
            if (_model.LYDO_ACTIVE == "1")
            {
                _model.LYDO_ACTIVE = "Hiệu Lực";
            }
            return PartialView("Detail", _model);
        }
        #endregion
    }
}